package com.yachang.qym.usbhost.activity;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.TextView;

import com.yachang.qym.usbhost.R;
import com.yachang.qym.usbhost.utils.AllData;

public class MachineInfoActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private TextView mMachineID,mMachineName,mMachineOtherName,mMachineNumber,mMachineVersion,mSoftwareVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machine_info);
        initView();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    private void initView(){
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("固件信息");
        mMachineID = (TextView) findViewById(R.id.tv_machine_id);
        mMachineName = (TextView) findViewById(R.id.tv_machine_name);
        mMachineOtherName = (TextView) findViewById(R.id.tv_machine_other_name);
        mMachineNumber = (TextView) findViewById(R.id.tv_machine_number);
        mMachineVersion = (TextView) findViewById(R.id.tv_machine_version);
        mSoftwareVersion = (TextView) findViewById(R.id.tv_software_version);
        bindData();
    }

    private void bindData(){
        mMachineID.setText(AllData.machineInfo.getMachineId());
        mMachineName.setText(AllData.machineInfo.getMachineName());
        mMachineNumber.setText(AllData.machineInfo.getMachineNumber());
        mMachineOtherName.setText(AllData.machineInfo.getMachineOtherName());
        mMachineVersion.setText(AllData.machineInfo.getMachineVersion());
        mSoftwareVersion.setText(getAppVersionName(getApplicationContext()));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home :
                MachineInfoActivity.this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private String getAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo("com.yachang.qym.usbhost", 0);
            versionName = packageInfo.versionName;
            if (TextUtils.isEmpty(versionName)) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }
}
